<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;销售管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-order">&nbsp;&nbsp;合同管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
      <el-form ref="form" :model="agreementForm">
        <el-form-item class="agreement_formSubmit_search">
          <el-select v-model="agreementForm.name" placeholder="请选择客户名称"
                     @change="loadAgreementList()">
            <el-option
                v-for="item in agreementOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="agreement.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="name" label="客户名称" width="200px"></el-table-column>
        <el-table-column prop="salesType" label="销售类型" width="200px"></el-table-column>
        <el-table-column prop="dateSale" label="销售日期" width="200px"></el-table-column>
        <el-table-column prop="oddNumber" label="单号" width="200px"></el-table-column>
        <el-table-column prop="sales" label="销售金额" width="200px"></el-table-column>
        <el-table-column prop="drawback" label="退款金额" width="200px"></el-table-column>
        <el-table-column prop="sum" label="总计金额" width="200px"></el-table-column>
        <el-table-column prop="deduction" label="扣款" width="200px"></el-table-column>
        <el-table-column prop="collection" label="回款" width="200px"></el-table-column>
        <el-table-column prop="dateColl" label="回款日期" width="200px"></el-table-column>
        <el-table-column prop="dateReceive" label="应收到账期" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="agreement.length">
        </el-pagination>
      </div>
      <!-- 以下是弹出的修改固定资产的对话框 -->
      <el-dialog
          width="80%"
          :modal-append-to-body="false"
          title="编辑合同"
          :visible.sync="dialogFormVisible">
        <el-form :model="agreementForm">
          <el-form-item label="id" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="客户名称" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="销售类型" :label-width="formLabelWidth">
            <el-select v-model="agreementForm.salesType" placeholder="请选择分类">
              <el-option
                  v-for="item in salesTypes"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="销售日期" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.dateSale" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="单号" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.oddNumber" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="销售金额" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.sales" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="退款金额" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.drawback" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="总计金额" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.sum" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="扣款" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.deduction" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="回款" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.collection" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="回款日期" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.dateColl" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="应收到账日期" :label-width="formLabelWidth">
            <el-input v-model="agreementForm.dateReceive" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" type="warning" style="color:white;">取 消</el-button>
          <el-button type="primary" @click="submitEditForm()" style="color:white;">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {

      dialogFormVisible:false,
      salesTypes: [
        {
          label: "支付宝",
          value: "支付宝"
        },
        {
          label: "微信",
          value: "微信"
        },
        {
          label: "公司账号",
          value: "公司账号"
        },
        {
          label: "转账",
          value: "转账"
        },
        {
          label: "现金",
          value: "现金"
        },
      ],
      formLabelWidth: '120px',
      agreementOptions: [],
      agreement: [],
      agreementForm: {
        id: '',
        name: '',
        salesType: '',
        dateSale: '',
        oddNumber: '',
        sales: '',
        drawback: '',
        sum: '',
        deduction: '',
        collection: '',
        dateColl: '',
        dateReceive: '',
      },
      currentPage: 1,
      pageSize: 10,
    }
  },
  methods: {
    loadAgreementList() {
      let url = 'http://localhost:9080/customAgreements';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.agreement = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openEditDialog(agreement) {
      let url = 'http://localhost:9080/customAgreements/' + agreement.id;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.agreementForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAgreementList();
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9080/customAgreements/' + this.agreementForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.agreementForm);
      console.log('formData = ' + formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改合同成功！',
            type: 'success'
          });
          this.loadAgreementList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAgreementList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },

    /*表单删除*/
    openDeleteConfirm(agreement) {
      let title = '提示';
      let message = '此操作将永久删除【' + agreement.name + '】属性, 是否继续?';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(agreement);
      }).catch(() => {
      });
    },
    handleDelete(agreement) {
      let url = 'http://localhost:9080/customAgreements/' + agreement.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除属性【' + agreement.name + '】成功！',
            type: 'success'
          });
          this.loadAgreementList();
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAgreementList();
            }
          });
        }
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadAgreementList();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.agreement_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>